package com.wisezone.workflow.exec;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.wisezone.common.WebContextMgr;
import com.wisezone.common.utils.DateUtils;
import com.wisezone.common.utils.ValueUtils;
import com.wisezone.workflow.core.ActivityInstance;
import com.wisezone.workflow.core.FlowEngine;
import com.wisezone.workflow.exception.WfDaoException;
import com.wisezone.workflow.exception.WfExecException;

/**
 * 活动结束定时器 2010-10-25
 * @author tss 
 *
 */
public class WfTimerTask extends TimerTask {

	private Integer timerPeriod;
	
	private ActivityInstance activityInst;
	
	private boolean isRun = true;
	
	private Timer timer;
	
	public WfTimerTask(Integer timerPeriod,ActivityInstance activityInst,Timer timer) {
		this.timerPeriod = timerPeriod;
		this.activityInst = activityInst;
		this.timer = timer;
	} 
	
	@Override
	public void run() {
		
		if(isRun){
			
			//获得间隔时间（单位毫秒数）
			Date endTime = null;
			try {
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(c.getTimeInMillis() + timerPeriod);
				endTime = c.getTime();
			} catch (Exception e) {
				throw new WfDaoException("nodeExecTime 参数不合法");
			}
			Date startTime = activityInst.getModel().getCreateTime();
			//endTime = DateUtils.addTime(startTime, endTime);
			startTime = new Date();
			int index = DateUtils.compareTime(startTime, endTime);
			if(index <= 0){  //结束时间到
				
				//获得下一操作人集合，或操作人组集合
				//获得途径1：从活动实例变量中得到
				//途径2： 从定义方法中得到（instance.method(args))
				
				Long [] nextActorIds = null;
				Long [] nextActorGroupIds = null;
				
				Object createType = activityInst.getVariable(FlowEngine.CREATE_TYPE);
				
				//途径1：
				if(createType == null){
					
					Object actorValue = activityInst.getVariable(FlowEngine.ACTOR);
					Object actorGroupValue = activityInst.getVariable(FlowEngine.ACTOR_GROUP);
					if(actorValue != null){
						nextActorIds = ValueUtils.valueToArrLong(actorValue);
					}
					if(actorGroupValue != null){
						
						nextActorGroupIds = ValueUtils.valueToArrLong(actorGroupValue);
					}
					
				//途径2：	
				}else{
					
					WfGetUserTimerTask getUserTimerTask = null;
					
					//Java class 创建方式
					if(createType.toString().equals(FlowEngine.CREATE_TYPE_JAVA)){
						Object className = activityInst.getVariable(FlowEngine.CLASS_NAME);
						if(className == null){
							throw new WfExecException("定时器获得用户实现创建类不能为空");
						}
						try {
							Class getUserClass = Class.forName(className.toString());
							getUserTimerTask = (WfGetUserTimerTask) getUserClass.newInstance();
						} catch (Exception e) {
							throw new WfExecException("定时器获得用户实现创建类实例化失败,类名为："+className);
						}
						
					//spring 容器创建方式	
					}else if(createType.toString().equals(FlowEngine.CREATE_TYPE_SRPING)){
						
						Object beanName = activityInst.getVariable(FlowEngine.BEAN_NAME);
						if(beanName == null){
							throw new WfExecException("定时器获得用户实现beanName不能为空");
						}
						getUserTimerTask = WebContextMgr.getBean(beanName.toString());
						if(getUserTimerTask == null){
							throw new WfExecException("定时器获得用户实现找不到beanName实例,beanName为："+beanName);
						}
					}else{
						throw new WfExecException("定时器 下一操作人获得创建类型找不到，必须是" +
								"java 或 spring 类型");
					}
					
					//获得下一活动操作用户集合
					nextActorIds = getUserTimerTask.getActorIds(activityInst);
					nextActorGroupIds = getUserTimerTask.getActorGroupIds(activityInst);
				}

				//活动结束
				activityInst.end(nextActorIds, nextActorGroupIds);
				
				 //关闭
				timer.cancel();  
			}
		}
		
	}
	
	public static void main(String[] args) {
		
		Calendar c = Calendar.getInstance();
		Long l = c.getTimeInMillis() ;//+ c.getTimeInMillis();
		System.out.println(l/1000/60/24/365);
		System.out.println(c.getTime());
		c.setTimeInMillis(l);
		System.out.println(c.getTime());
	}

}
